package com.mofang.powerdekorhelper.activity.mine;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.adapter.MinePointListAdater;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewAdapter;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewList;
import com.mofang.powerdekorhelper.model.MinePoint;
import com.mofang.powerdekorhelper.model.ResultMessage2;
import com.mofang.powerdekorhelper.model.ResultMessage4;
import com.mofang.powerdekorhelper.persenter.MinePointPersenter;
import com.mofang.powerdekorhelper.utils.SharePerforenceUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.MinePointView;
import com.mofang.powerdekorhelper.witget.MySwipeRefreshLayout;
import com.mofang.powerdekorhelper.witget.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 我的积分
 * Created by luofei on 2016/9/26 0026.
 */
public class MinePointActivity extends MvpActivity<MinePointView, MinePointPersenter> implements
        SwipeRefreshLayout.OnRefreshListener,
        BaseRecycleViewList.MyRecycleOnScrollListener,
        MinePointView {

    @BindView(R.id.mine_point_titlebar)
    TitleBar mTitleBar;

    @BindView(R.id.mine_point_totle)
    TextView pointTotleTv;

    @BindView(R.id.sign_imv)
    ImageView signBgImv;

    @BindView(R.id.sign_success_bg)
    RelativeLayout signSuccessBg;

    @BindView(R.id.sign_point_tv)
    TextView signPointTv;

    //签到成功，积分+1
    private int pageSize = 10;
    private int currentPage = 1;

    @BindView(R.id.mine_point_swipe_refresh)
    MySwipeRefreshLayout swipeRefresh;

    @BindView(R.id.mine_point_list)
    BaseRecycleViewList minePointRecycle;

    int userId;

    int integralType;

    @Override
    public MinePointPersenter initPresenter() {
        return new MinePointPersenter();
    }

    @Override
    public void initView() {
        inflateLayout(R.layout.activity_minepoint_layout);
        initTitleBar();
        swipeRefresh.setOnRefreshListener(this);
    }

    @Override
    public void initData() {
        userId = SharePerforenceUtils.getInstance(this).getUserID();
        presenter.getTotalPoint(userId);
        presenter.getIsSign(userId);
        initPintList();
    }

    private void initPintList() {
        List<MinePoint> minePointList = new ArrayList<>();
        minePointList.add(new MinePoint(4, "首次登录积分"));
        minePointList.add(new MinePoint(1, "分享积分"));
        minePointList.add(new MinePoint(3, "客户积分"));
        minePointList.add(new MinePoint(2, "订单积分"));
        minePointList.add(new MinePoint(5, "沟通积分"));
        minePointList.add(new MinePoint(6, "签到积分"));
        MinePointListAdater adater = new MinePointListAdater(minePointList, this, R.layout.point_type_list_item_layout);
        adater.setOnItemClickListner(new BaseRecycleViewAdapter.OnItemClickListner() {
            @Override
            public void onItemClickListner(ViewGroup parent, View v, int position) {
                MinePoint minePoint = (MinePoint) v.getTag();
                integralType = minePoint.getType();

                startActivity(new Intent(MinePointActivity.this, MinePointDetailActivity.class)
                        .putExtra("IntegralType", integralType));
            }
        });
        minePointRecycle.setAdapter(adater);
    }

    @Override
    public void setTotalPoint(ResultMessage2 totalPoint) {
        pointTotleTv.setText(totalPoint.getResult() + "");
        hideProgress();
    }

    @Override
    public void onRefresh() {
        currentPage = 1;
        presenter.getTotalPoint(userId);
        presenter.getIsSign(userId);
    }

    @OnClick({R.id.sign_imv, R.id.sign_success_bg})
    void onclick(View view) {

        switch (view.getId()) {

            case R.id.sign_imv:
                presenter.getSign(userId);
                break;

            case R.id.sign_success_bg:
                signSuccessBg.setVisibility(View.GONE);
                break;
        }
    }

    @Override
    public void setIsSign(boolean isSign) {
        if (isSign) {
            signBgImv.setImageResource(R.mipmap.sign_icon);
            signBgImv.setEnabled(true);
        } else {
            signBgImv.setImageResource(R.mipmap.already_sign_icoon);
            signBgImv.setEnabled(false);
        }
        hideProgress();
    }

    @Override
    public void setSign(ResultMessage4 resultMessage) {
        signBgImv.setImageResource(R.mipmap.already_sign_icoon);
        signBgImv.setEnabled(false);
        signPointTv.setText("签到成功，积分+" + resultMessage.getResult());
        signSuccessBg.setVisibility(View.VISIBLE);
        presenter.getTotalPoint(userId);
        handler.sendEmptyMessageDelayed(0, 2000);
        hideProgress();
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            signSuccessBg.setVisibility(View.GONE);
        }
    };

    private void initTitleBar() {

        initTitleBarWithback(mTitleBar, R.string.mine_point_mall);

      /*  mTitleBar.addAction(new TitleBar.TextAction("积分说明") {
            @Override
            public void performAction(View view) {
                //startActivity(new Intent(MinePointActivity.this, PointDescriptionActivity.class));
            }
        });*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    // 加载更多
    @Override
    public void loadMore() {
        currentPage++;
    }

    @Override
    public void showProgress() {
        // swipeRefresh.show();
    }

    @Override
    public void hideProgress() {
        swipeRefresh.hide();
    }

    @Override
    public void toast(CharSequence s) {
        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {
        T.showShort(this, getResources().getString(id));
    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {
        hideProgress();
    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {
        hideProgress();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }
}
